package com.example.service;

import com.example.config.UserHystrixCommand;
import com.example.modle.ResponseInfo;
import com.example.modle.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import rx.Observable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

@Service
public class HttpService {

    @Value("${hystrix.executionTimeoutInMilliseconds}")
    private Integer executionTimeoutInMilliseconds;
    @Value("${hystrix.executionIsolationSemaphoreMaxConcurrentRequests}")
    private Integer executionIsolationSemaphoreMaxConcurrentRequests;


    private Map<String,String> cacheMap=new ConcurrentHashMap<>();

    @Autowired
    private HttpClient httpClient;

    public Observable<ResponseInfo> getUser() {

        Supplier<Observable<Map<String,Object>>> supplier=()->httpClient.getUser();

        Observable<ResponseInfo> ret = new UserHystrixCommand(supplier, executionTimeoutInMilliseconds, executionIsolationSemaphoreMaxConcurrentRequests)
                .toObservable().map(response -> {

                    ResponseInfo responseInfo = new ResponseInfo();
                    responseInfo.setHttpStatus(HttpStatus.valueOf((Integer) response.get("downstream-resp-status")));
                    if ((Integer) response.get("downstream-resp-status") != 200) {
                        responseInfo.setReason(ResponseInfo.Reason.builder()
                                .code(response.get("downstream-resp-status").toString())
                                .message(response.get("message").toString())

                                .build());


                    } else {

                        responseInfo.setT(User.builder()
                                .name(response.get("name").toString())
                                .age((Integer) response.get("age"))
                                .build());
                    }
                    return responseInfo;

                });
        System.out.println("httpService getUser() "+ Thread.currentThread().getName());
        return ret;
    }


    public Observable<ResponseInfo> getUser01() {
        Supplier<Observable<Map<String,Object>>> supplier=()->httpClient.getUser01();
        Observable<ResponseInfo> ret = new UserHystrixCommand(supplier, executionTimeoutInMilliseconds, executionIsolationSemaphoreMaxConcurrentRequests)
                .toObservable().map(response -> {

                    ResponseInfo responseInfo = new ResponseInfo();
                    responseInfo.setHttpStatus(HttpStatus.valueOf((Integer) response.get("downstream-resp-status")));
                    if ((Integer) response.get("downstream-resp-status") != 200) {
                        responseInfo.setReason(ResponseInfo.Reason.builder()
                                .code(response.get("downstream-resp-status").toString())
                                .message(response.get("message").toString())

                                .build());
                    } else {

                        responseInfo.setT(User.builder()
                                .name(response.get("name").toString())
                                .age((Integer) response.get("age"))
                                .build());
                    }
                    return responseInfo;

                });
        System.out.println("httpService getUser01() "+ Thread.currentThread().getName());
        return ret;
    }

    public String asyncTest(){
        System.out.println("httpService async test thread: "+ Thread.currentThread().getName());
        httpClient.asyncTest();
        return "async test";
    }

    @Scheduled(
            initialDelayString = "3000",
            fixedDelayString = "3000"
    )
    public Map scheduledBuild(){
        cacheMap.put("randTime", System.nanoTime()+"");
        System.out.println("**************"+ cacheMap.get("randTime")+"*************");
        return cacheMap;
    }

}
